home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Dialectic 1.2 / source / Dialectic ƒ / Shell ƒ / apple events.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  4.3 KB  |  140 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        apple events.c
  4.  
  5. Purpose:    This module handles the 4 required apple events: open
  6.             application, open document, print document (not supported),
  7.             and quit application.
  8.  
  9. \**********************************************************************/
  10.  
  11. #include "apple events.h"
  12. #include "environment.h"
  13. #include "dialectic generic open.h"
  14.  
  15. /*-----------------------------------------------------------------------------------*/
  16. /* internal stuff for apple events.c                                                 */
  17. /* (need to be declared here because SetUpAppleEvents() references them              */
  18.  
  19. pascal OSErr HandleOpenAppAE(const AppleEvent *theAppleEvent, const AppleEvent *reply,
  20.     long refcon);
  21. pascal OSErr HandleOpenDocAE(const AppleEvent *theAppleEvent, const AppleEvent *reply,
  22.     long refcon);
  23. pascal OSErr HandlePrintDocAE(const AppleEvent *theAppleEvent, const AppleEvent *reply,
  24.     long refcon);
  25. pascal OSErr HandleQuitAE(const AppleEvent *theAppleEvent, const AppleEvent *reply,
  26.     long refcon);
  27. pascal OSErr MyGotRequiredParams(const AppleEvent *theAppleEvent);
  28.  
  29. void SetUpAppleEvents(void)
  30. /* called at program initialization, AFTER checking if apple events exist at all.
  31.    This is the magic linker that makes the procedures below get called when we
  32.    say AEProcessAppleEvent(&theEvent) in response to a kHighLevelEvent (see main.c). */
  33.  
  34. {
  35.     AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
  36.                                     HandleOpenAppAE, 0, FALSE);
  37.     AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
  38.                                     HandleOpenDocAE, 0, FALSE);
  39.     AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
  40.                                     HandlePrintDocAE, 0, FALSE);
  41.     AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
  42.                                     HandleQuitAE, 0, FALSE);
  43.     AESetInteractionAllowed(kAEInteractWithAll);    /* don't know what this does, sorry */
  44. }
  45.  
  46. /* Don't ask about the rest of this code.  It is a straight port from the pascal
  47.    code given in IM Interapplication Communication; not even the variable names
  48.    have been changed to protect the innocent.  I don't pretend to understand this,
  49.    but it works well enough to (1) not crash, (2) open and close the application
  50.    properly, and (3) get an FSSpec on open/print which we can then work on
  51.    directly (see the generic open.c file). */
  52.  
  53. pascal OSErr HandleOpenAppAE(const AppleEvent *theAppleEvent, const AppleEvent *reply,
  54.     long refcon)
  55. {
  56.     return MyGotRequiredParams(theAppleEvent);
  57. }
  58.  
  59. pascal OSErr HandleOpenDocAE(const AppleEvent *theAppleEvent, const AppleEvent *reply,
  60.     long refcon)
  61. {
  62.     OSErr            isHuman, dummy;
  63.     FSSpec            myFSS;
  64.     AEDescList        docList;
  65.     long            index, itemsInList;
  66.     Size            actualSize;
  67.     AEKeyword        keywd;
  68.     DescType        returnedType;
  69.     
  70.     isHuman=AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &docList);
  71.     if (isHuman==noErr)
  72.     {
  73.         if (MyGotRequiredParams(theAppleEvent)==noErr)
  74.         {
  75.             if (AECountItems(&docList, &itemsInList)==noErr)
  76.             {
  77.                 for (index=1; index<=itemsInList; index++)
  78.                 {
  79.                     if (AEGetNthPtr(&docList, index, typeFSS, &keywd, &returnedType,
  80.                         &myFSS, sizeof(myFSS), &actualSize)==noErr)
  81.                     {
  82.                         GenericOpen(&myFSS);        /* in rc generic open.c */
  83.                     }
  84.                     else
  85.                     {
  86.                         // handle error getting Nth pointer
  87.                     }
  88.                 }
  89.             }
  90.             else
  91.             {
  92.                 // handle error counting items
  93.             }
  94.         }
  95.         else
  96.         {
  97.             // handle error from MyGetRequiredParams
  98.             dummy=AEDisposeDesc(&docList);
  99.         }
  100.     }
  101.     else
  102.     {
  103.         // handle error getting direct parameter
  104.     }
  105.     
  106.     return isHuman;
  107. }
  108.  
  109. pascal OSErr HandlePrintDocAE(const AppleEvent *theAppleEvent, const AppleEvent *reply,
  110.     long refcon)
  111. {
  112.     return errAEEventNotHandled;
  113. }
  114.  
  115. pascal OSErr HandleQuitAE(const AppleEvent *theAppleEvent, const AppleEvent *reply,
  116.     long refcon)
  117. {
  118.     OSErr            isHuman;
  119.     
  120.     isHuman=MyGotRequiredParams(theAppleEvent);
  121.     if (isHuman==noErr)
  122.         gDone=TRUE;    /* our global program-done flag, so we will exit our event loop */
  123.                     /* do NOT under any circumstances call ExitToShell() here; */
  124.                     /* be patient and wait to get back to the event loop, which */
  125.                     /* will exit the next time around now than gDone=TRUE */
  126.     
  127.     return isHuman;
  128. }
  129.  
  130. pascal OSErr MyGotRequiredParams(const AppleEvent *theAppleEvent)
  131. {
  132.     DescType        returnedType;
  133.     Size            actualSize;
  134.     
  135.     /* yeah, whatever */
  136.     return (AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
  137.             &returnedType, 0L, 0, &actualSize)==errAEDescNotFound) ? noErr :
  138.             errAEParamMissed;
  139. }
  140.